//
// Created by mac on 2019/6/14.
// Copyright (c) 2019 Facebook. All rights reserved.
//

//#import <D2dPlayer/D2dPlayer.h>
#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>

#import "RND2dPlayerManager.h"

static RND2dPlayer *rnd2dPlayer = nil;

@interface RND2dPlayerManager()
//@property(nonatomic,strong)D2dPlayer *d2dPlayer;
//@property(nonatomic,strong)RND2dPlayer *rnd2dPlayer;
@end
@implementation RND2dPlayerManager {

}

RCT_EXPORT_MODULE()

RCT_EXPORT_VIEW_PROPERTY(onPlaying, RCTBubblingEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onStopped, RCTBubblingEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onVout, RCTBubblingEventBlock);

- (UIView *)view
{

//    _d2dPlayer = [[D2dPlayer alloc]init];
//    return _d2dPlayer;

    rnd2dPlayer = [[RND2dPlayer alloc] init];
    return rnd2dPlayer;
}

+ (RND2dPlayer*)rnd2dPlayer{
    return rnd2dPlayer;
}

RCT_EXPORT_METHOD(play:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
//    [_d2dPlayer play];
    [rnd2dPlayer play];
    resolve(nil);
}

RCT_EXPORT_METHOD(stop:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
//    [_d2dPlayer pause];
    [rnd2dPlayer pause];
    resolve(nil);
}

RCT_EXPORT_METHOD(mute:(BOOL)muted resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
//    [_d2dPlayer mute:muted];
    [rnd2dPlayer mute:muted];
    resolve(nil);
}

RCT_EXPORT_METHOD(takeSnapshot:(NSString*)path resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        BOOL result = [rnd2dPlayer takeSnapshot:path];
        resolve([NSNumber numberWithBool: result]);
    });
}
@end